﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

public partial class Reg_ChangePassword : System.Web.UI.Page
{
    SendEmails autoMail = new SendEmails();
    RegistrationData _objRegistrationData = new RegistrationData();
    string Link = string.Empty, strCode = string.Empty;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack)
        {
            
            if (!(String.IsNullOrEmpty(txtNewPassword.Text.Trim())))
            {
                txtNewPassword.Attributes["value"] = txtNewPassword.Text;
            }
            if (!(String.IsNullOrEmpty(txtCPass.Text.Trim())))
            {
                txtCPass.Attributes["value"] = txtCPass.Text;
            }
        
        }

        if (!object.Equals(Session["User"], null))
        {
            _objRegistrationData = Session["User"] as RegistrationData;

            divMain.Visible = true;
            divMessage.Visible = false;

        }
        else
        {
            divMain.Visible = false;
            divMessage.Visible = true;
        }
    }

    protected void btnSave_Click(object sender, ImageClickEventArgs e)
    {
        if (txtNewPassword.Text == string.Empty)
        {
            lblMessage.Text = "Please Enter a valid new Password";
        }
        else if (txtCPass.Text == string.Empty)
        {
            lblMessage.Text = "Please confirm your password";

        }
        else if (txtCPass.Text != txtNewPassword.Text)
        {
            lblMessage.Text = "Your new password and confirm password do not match.";
        }
        else
        {
            bool bflag = false;
            string salt = string.Empty, Password = string.Empty;
            salt = _objRegistrationData.RandomString(5);
            Password = _objRegistrationData.protectpassword(_objRegistrationData.protectpassword(txtNewPassword.Text) + salt);
            bflag = _objRegistrationData.Update_ResetPassword(_objRegistrationData.Email, Password, salt);
            if (bflag == true)
            {
                lblMessage.Text = "Your Password has been changed successfully.";
                txtCPass.Attributes["value"] = string.Empty;
                txtNewPassword.Attributes["value"] = string.Empty;
            }
            else
            {
                lblMessage.Text = "Sorry some error occured please try again after some time.";
            }
        }
    }


}